#include "niv4.h"
#include "main.h"

void ajouterProtocole(Apps app, Stats* stats)
{
    if (stats->app[app]==0)
    {
        stats->app[app]=1;
        switch (app) 
        {
            case APP_DROPBOX : printM("Le logiciel Dropbox est lancée \"quelque part\" dans le réseau\n");break;
            case APP_MESS : printM("Messagerie instantanée\n");break;
            case APP_NET : printM("Navigation internet\n");break;
            case APP_MAIL : printM("Email\n");break;
            case APP_MAX : break;

        }
    }
}

void printTCP(const u_char* buff, Stats* stats)
{
    const struct tcphdr *tcp_hdr = (const struct tcphdr*)(buff);
    int portSource = htons(tcp_hdr->source);
    int portDest = htons(tcp_hdr->dest);
    int portPrinc = MIN(portSource,portDest);

    printD("TCP\n");
    switch (portPrinc) {
        case PORT_TCP_FTP :
        case PORT_TCP_FTP2 : printD("FTP\n"); break;
        case PORT_TCP_SSH : printD("SSH\n"); break;
        case PORT_TCP_TELNET : printD("Telnet\n"); break;
        case PORT_TCP_SMTP : printD("SMTP\n"); break;
        case PORT_TCP_HTTP : printD("HTTP\n");ajouterProtocole(APP_NET,stats); break;
        case PORT_TCP_POP3 : printD("POP3\n"); ajouterProtocole(APP_MAIL,stats);break;
        case PORT_TCP_SFTP : printD("SFTP\n"); break;
        case PORT_TCP_IMAP3 : 
        case PORT_TCP_IMAP : printD("IMAP\n"); ajouterProtocole(APP_MAIL,stats);break;
        case PORT_TCP_BGP : printD("BGP\n"); break;
        case PORT_TCP_IRC : printD("IRC\n"); break;
        case PORT_TCP_LDAP : printD("LDAP\n"); break;
        case PORT_TCP_HTTPS : printD("HTTPS\n"); ajouterProtocole(APP_NET,stats);break;
        case PORT_TCP_MSN : printD("MSN\n");ajouterProtocole(APP_MESS,stats); break;
        case PORT_TCP_JABBER :
        case PORT_TCP_JABBER2 : printD("JABBER\n"); ajouterProtocole(APP_MESS,stats);break;
        case PORT_TCP_SIP : printD("SIP\n"); break;
        case PORT_TCP_IRC_SERV :
        case PORT_TCP_IRC_SERV_SSL :
        case PORT_TCP_IRC_SERV_SSL2 : printD("IRC_SERV\n"); break;
        case PORT_TCP_NETBIOS :
        case PORT_TCP_NETBIOS1 :
        case PORT_TCP_NETBIOS2 :
        case PORT_TCP_NETBIOS3 :
        case PORT_TCP_NETBIOS4 : printD("NETBIOS\n");break;
        default : printD("Port TCP inconnu \n");
    }
    // printf(" port source : %d port destination : %d\n",htons(tcp_hdr->source),htons(tcp_hdr->dest));
}


void printUDP(const u_char* buff, Stats* stats)
{
    const struct udphdr *udp_hdr = (const struct udphdr*)(buff);
    int portSource = htons(udp_hdr->source);
    int portDest = htons(udp_hdr->dest);
    int portPrinc = MIN(portSource,portDest);

    printD("UDP\n");
    switch (portPrinc) {
        case PORT_UDP_DNS : 
        case PORT_UDP_MDNS : printD("DNS\n"); break;
        case PORT_UDP_NTP : printD("NTP\n"); break;
        case PORT_UDP_SNMP : printD("SNMP\n"); break;
        case PORT_UDP_DHCP : printD("DHCP\n"); break;
        case PORT_UDP_DROPBOX : printD("DROPBOX\n");ajouterProtocole(APP_DROPBOX,stats); break;
        case PORT_UDP_SSDP : printD("SSDP\n");break;
        default : printD("Port UDP inconnu \n");
    }
    //printf(" port source : %d port destination : %d\n",htons(udp_hdr->source),htons(udp_hdr->dest));
}
